package com.gdk.ksoft.ui.works.adapters;

import android.content.Context;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.TextView;

import com.gdk.ksoft.R;
import com.gdk.ksoft.data.entry.tables.CustomerDebtTable;
import com.gdk.ksoft.assembly.utils.extensions.FormatUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by hel on 2014-12-04.
 * 收欠款页面收据适配器
 */
public class PcustomerDebtAdapter extends BaseAdapter {

    /**
     * 选择的项发生改变时
     */
    public interface OnCheckValueChangedListener{
        public void onCheckValueChanged(HashMap<Integer,Boolean> selector,int checkedNum,int totalNum,double payableMoney,double totalMoney);
    }

    private Context context;
    private List<CustomerDebtTable> lst;
    public HashMap<Integer,Boolean> RecorderSelector = new HashMap<>();
    private OnCheckValueChangedListener onCheckValueChangedListener;
    private double allMoney = 0;//用于计算总金额

    public PcustomerDebtAdapter(Context context,List<CustomerDebtTable> lst,OnCheckValueChangedListener listener){
        this.context = context;
        this.lst = lst;
        if(this.lst!=null && this.lst.size()>0){
            for(CustomerDebtTable item : this.lst)
                allMoney += item.wsje;
        }
        this.onCheckValueChangedListener = listener;
    }

    @Override
    public int getCount() {
        return this.lst == null?0:this.lst.size();
    }

    @Override
    public Object getItem(int position) {
        return this.lst.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        final CustomerDebtTable item = this.lst.get(position);
        final ViewHolder holder;
        if(convertView == null){
            holder = new ViewHolder();
            convertView = View.inflate(context, R.layout.item_pcustomerdebt_lstdebt,null);
            holder.cbSelector = (CheckBox)convertView.findViewById(R.id.item_pcustomerdebt_lstdebt_cbSelector);
            holder.txtOrderNo = (TextView)convertView.findViewById(R.id.item_pcustomerdebt_lstdebt_txtOrderNo);
            holder.txtDebtMoney = (TextView)convertView.findViewById(R.id.item_pcustomerdebt_lstdebt_txtDebtMoney);
            holder.etPayableMoney = (EditText)convertView.findViewById(R.id.item_pcustomerdebt_lstdebt_etPayableMoney);
            convertView.setTag(holder);
        }else{
            holder = (ViewHolder)convertView.getTag();
        }
        holder.txtOrderNo.setText("销售单号："+item.djh);
        holder.txtDebtMoney.setText("未收金额："+ FormatUtils.exactNumber(item.wsje,"0.00")+"元");
        holder.etPayableMoney.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
            }

            @Override
            public void afterTextChanged(Editable s) {
                String payableMoneyStr = holder.etPayableMoney.getText().toString();
                if (TextUtils.isEmpty(payableMoneyStr)||payableMoneyStr.equals("-"))
                    payableMoneyStr = "0.00";
                float payableMoney = Float.parseFloat(payableMoneyStr);
//                if(Float.compare(item.wsje,payableMoney)<0)
//                    Toast.makeText(context,"收款金额不能大于未收金额!",Toast.LENGTH_SHORT).show();
                item.payableMoney = payableMoney;
                lst.set(position, item);//修改列表数据
                postData();//发送数据
            }
        });
        holder.cbSelector.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                RecorderSelector.put(position,isChecked);
                postData();//发送数据
            }
        });
        holder.etPayableMoney.setText(FormatUtils.exactNumber(item.wsje,"0.00"));//默认为未收金额
        if(RecorderSelector.containsKey(position) && RecorderSelector.get(position))
            holder.cbSelector.setChecked(RecorderSelector.get(position));
        else{
            RecorderSelector.put(position,false);
            holder.cbSelector.setChecked(false);
        }
        return convertView;
    }

    /**
     * 发送数据
     */
    public void postData(){
        if(onCheckValueChangedListener!=null) {
            HashMap<String, Object> value = getDebtInfo();
            int count = Integer.parseInt(value.get("selectedCount").toString());
            double totalMoney = Double.parseDouble(value.get("totalMoney").toString());
            onCheckValueChangedListener.onCheckValueChanged(RecorderSelector,count, lst.size(), totalMoney, allMoney);
        }
    }

    /**
     * 获取勾选过的数据
     * @return
     */
    public HashMap<String,Object> getDebtInfo(){
        HashMap<String,Object> value = new HashMap<>();
        double selectedTotalMoney = 0;
        int selectedCount = 0;
        for(Map.Entry<Integer,Boolean> item : RecorderSelector.entrySet()){
            if(item.getValue() == true){
                selectedCount++;
                selectedTotalMoney+=lst.get(item.getKey()).payableMoney;
            }
        }
        value.put("totalMoney",selectedTotalMoney);
        value.put("selectedCount",selectedCount);
        return value;
    }

    /**
     * 获取适配器的数据源
     * @return
     */
    public List<CustomerDebtTable> getDataSource(){
        return this.lst;
    }

    class ViewHolder{
        public CheckBox cbSelector;
        public TextView txtOrderNo;
        public TextView txtDebtMoney;
        public EditText etPayableMoney;
    }

}
